#include "AppMenuCommentsPanel.h"

#include <QLinearGradient>
#include <QStylePainter>

AppMenuCommentsPanel::AppMenuCommentsPanel(QWidget* parent) : QWidget(parent) {
  setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
  setFixedWidth(228);
}

void AppMenuCommentsPanel::setComments(const MenuComments& c) {
  m_comments = c;
}

void AppMenuCommentsPanel::paintEvent(QPaintEvent* event) {
  QStylePainter painter(this);

  // background
  QLinearGradient lg(width() / 2, 0, width() / 2, height());
  lg.setColorAt(0, QColor(0x0B4D52));
  lg.setColorAt(1, QColor(0x1E322F));

  QRect r = rect();
  r.adjust(0, 8, -8, -8);  // parent's border
  painter.fillRect(r, lg);

  // Title
  QFont f = font();
  f.setBold(true);
  f.setPixelSize(15);

  painter.setFont(f);
  painter.setPen(QColor(0x458C81));

  QRect txtR = painter.fontMetrics().tightBoundingRect(m_comments.title);
  QRect rcTitle(r);
  rcTitle.adjust(0, 5, 0, 0);
  rcTitle.setBottom(rcTitle.top() + txtR.height());
  painter.drawText(rcTitle, m_comments.title, QTextOption(Qt::AlignCenter));

  f.setBold(false);
  f.setPixelSize(12);
  painter.setFont(f);

  r.adjust(16, txtR.height() * 2, -10, 0);
  for (int i = 0; i < m_comments.comments.size(); ++i) {
    QString comment = m_comments.comments[i].first;
    painter.setPen(Qt::NoPen);
    painter.setBrush(QColor(0x3E655F));

    QRect circleR(r.topLeft() + QPoint(0, 5), QSize(7, 7));
    painter.drawEllipse(circleR);

    QRect txtRect(r);
    txtRect.adjust(10, 0, 0, 0);

    painter.setBrush(Qt::NoBrush);
    painter.setPen(m_comments.comments[i].second);
    painter.drawText(txtRect, Qt::AlignTop | Qt::TextWordWrap, comment,
                     &txtRect);

    r.adjust(0, txtRect.height() + 8, 0, 0);
  }
}
